package cn.migu.music.hangupsms.cachemanage.handler;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.migu.music.common.utils.LogUtils;
import cn.migu.music.hangupsms.cachemanage.LocalCacheManager;
import cn.migu.music.hangupsms.cachemanage.event.AbsCacheEvent;
import cn.migu.music.hangupsms.cachemanage.event.ReLoadDbCacheEvent;
import lombok.extern.slf4j.Slf4j;

/**
 * <一句话描述>
 *
 * @author tanxiao
 * @date 2020/9/25 15:30
 */
@Component
@Slf4j
public class ReloadDbCacheHandler extends AbsCacheHandler<ReLoadDbCacheEvent> {
    @Autowired
    private LocalCacheManager                                localCacheManager;

    protected static final Map<String, ReloadDbCacheHandler> dbCacheHandlerMap = new HashMap<>();

    @PostConstruct
    @Override
    public void init() {
        AbsCacheHandler<AbsCacheEvent> handler = new AbsCacheHandler<AbsCacheEvent>() {
            @Override
            public void reload(AbsCacheEvent cacheEvent) {
                this.reload(cacheEvent);
            }
        };
        localCacheManager.putCache(ReLoadDbCacheEvent.CACHE_NAME, handler);
    }

    @Override
    public void reload(ReLoadDbCacheEvent event) {
        if (null == dbCacheHandlerMap.get(event.getCacheKey())) {
            LogUtils.warn(log, "没有找到数据库缓存更新处理器。 event=%s", event);
            return;
        }

        dbCacheHandlerMap.get(event.getCacheKey()).reload(event);
    }

    @Override
    public boolean equals(Object obj) {
        return super.equals(obj);
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }

}
